package edu.shiep.sunyu;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;

import edu.shiep.sunyu.model.Bus;
import edu.shiep.sunyu.model.DBClient;
import edu.shiep.sunyu.model.DBConstants;
import edu.shiep.sunyu.model.Database;
import edu.shiep.sunyu.model.Table;

public class AdminPreAddTimeToBusLineServlet extends HttpServlet {

	private static final long serialVersionUID = 3737190841502813766L;
	
	private DBClient _client = null;
	
	public void init(ServletConfig config) throws ServletException {
	   super.init(config);
	   
	   _client = new DBClient();
	   if (!_client.connect()) {
		   System.out.println("Can't connect to database!!!");
	   }
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		Database db = _client.getDB(DBConstants.Const_Database_Name);
		
		Table table = db.getTable(DBConstants.Const_Table_Bus_Name);
		
		DBCursor cursor = table.query();
		
		List lstBus = new ArrayList();
		
		while (cursor.hasNext()) { 
        	BasicDBObject doc = (BasicDBObject)cursor.next();
        	Bus bus = new Bus();
        	bus.deserialize(doc);
        	
        	lstBus.add(bus.getId());
        	System.out.println(doc);
        }
		
		request.setAttribute("buses", lstBus);
			
		RequestDispatcher view = request.getRequestDispatcher("AdminAddBusLineTime.jsp");
		view.forward(request, response);
	} 
	
}
